package com.itfreer.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.itfreer.ui.R;

/**
 * 定义确认对话框
 */
public class ConfirmDialog extends AlertDialog implements DialogInterface.OnClickListener{

	private DialogResult dialogResult = DialogResult.Cancel;

	/**
	 * 获取返回值
	 * @return
	 */
	public DialogResult getDialogResult(){
		return dialogResult;
	}

	private DialogCloseListener dialogCloseListener;

	/**
	 * 关闭事件
	 * @param dialogCloseListener
	 */
	public void setDialogCloseListener(DialogCloseListener dialogCloseListener){
		this.dialogCloseListener = dialogCloseListener;
	}

	public ConfirmDialog(Context context, String title, String dialogContent) {
		super(context, 0);

		Context themeContext = getContext();
		setButton(BUTTON_POSITIVE, "确 定", this);
		setButton(BUTTON_NEGATIVE, "取 消", this);
		setIcon(0);

		LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.com_itfreer_core_ui_dialog_confirmdialog, null);
		setView(view);

		TextView txtContent = (TextView) view.findViewById(R.id.txtContent);
		txtContent.setText(dialogContent);

		TextView titleView = (TextView) view.findViewById(R.id.confirmTitle);
		titleView.setText(title);
	}

	public void onClick(DialogInterface dialog, int which) {
		// Log.d(this.getClass().getSimpleName(), String.format("which:%d",
		// which));
		// 如果是“取 消”按钮，则返回，如果是“确 定”按钮，则往下执行
		if (which == BUTTON_POSITIVE){
			dialogResult = DialogResult.Ok;
			if(dialogCloseListener!=null){
				dialogCloseListener.OnClosed(this, dialogResult);
			}
			this.dismiss();
		} else {
			dialogResult = DialogResult.Cancel;
			if(dialogCloseListener!=null){
				dialogCloseListener.OnClosed(this, dialogResult);
			}
			this.dismiss();
		}
	}
}